import axios from 'axios'
import echarts from 'echarts/lib/echarts'
import 'echarts/lib/chart/line'
import 'echarts/lib/component/tooltip'
const labelConfig = [{
    type: 'tem',
    text: '日最高温度',
    unit: '°'
}, {
    type: 'wind',
    text: '日平均风速',
    unit: 'km/h'
}, {
    type: 'pre',
    text: '日累计降水',
    unit: 'mm'
}, {
    type: 'bui',
    text: '累积指数',
    unit: ''
}, {
    type: 'dc',
    text: '干旱码',
    unit: ''
}, {
    type: 'dmc',
    text: '粗腐殖质湿度码',
    unit: ''
}, {
    type: 'isi',
    text: '初始蔓延指数',
    unit: ''
}, {
    type: 'fwi',
    text: '天气指数',
    unit: ''
}, {
    type: 'ffmc',
    text: '细小可燃物湿度码',
    unit: ''
}, {
    type: 'hum',
    text: '日最小相对湿度',
    unit: '%'
}, {
    type: 'ffdi_index',
    text: '布隆-戴维斯指数',
    unit: ''
}, {
    type: 'kbdi',
    text: '干旱指数',
    unit: ''
}, {
    type: 'ffdi',
    text: '布隆-戴维斯',
    unit: ''
}]
// var allData = []
// function fetchData(url, data, callBack, failback) {

// }
async function requestPointData(lon, lat, errCallBack) {
    const data = await axios({
        type: 'get',
        // url: `http://139.9.243.34:8081/fireIndexCalculate/getFireIndexData?lon=${lon}&lat=${lat}`,
        // url: `http://192.168.3.144:8088/fireIndexCalculate/getFireIndexData?lon=${lon}&lat=${lat}`, // tdd本地
        url: `http://192.168.2.93:8088/fireIndexCalculate/getFireIndexData?lon=${lon}&lat=${lat}`, // 93
        responseType: 'json',
        timeout: 60000
    }).catch(() => {
        console.log('出现错误')
        errCallBack()
    })
    return data
}

function formateData(key, data) {
    const xAxis = []
    const value = []
    const config = labelConfig.find((e) => e.type === key)
    data.forEach((element) => {
        if (element) {
            xAxis.push(element.date.substring(4))
            value.push(element[key])
        }
    })
    return { xAxis, value, config }
}

function renderChart(target, labels, values) {
    var _text = target.text
    var _unit = target.unit
    console.log(_text)
    var option = {
        tooltip: {
            trigger: 'axis'
        },
        xAxis: [{
            type: 'category',
            data: labels,
            axisTick: {
                show: false
            },
            axisLine: {
                show: false
            },
            axisLabel: {
                show: true,
                color: '#999999',
                fontSize: 10,
                interval: 0,
                rotate: 0
            }
        }],
        yAxis: [{
            name: _text + _unit,
            nameTextStyle: {
                fontSize: 12,
                color: '#999999'
            },
            type: 'value',
            axisTick: {
                show: false
            },
            axisLine: {
                lineStyle: {
                    color: '#EDF0F3'
                },
                show: true
            },
            axisLabel: {
                color: '#999999',
                fontSize: 10,
                interval: 0
            },
            splitLine: {
                lineStyle: {
                    color: '#EDF0F3'
                }
            },
            splitNumber: 5

        }],
        grid: {
            left: 70,
            right: 20,
            top: 35,
            bottom: 45
        },
        series: [{
            name: _text,
            type: 'line',
            symbol: 'none',
            smooth: true,
            itemStyle: {
                normal: {
                    color: '#5252FB'
                }
            },
            lineStyle: {
                normal: {
                    color: '#5252FB'
                }
            },
            areaStyle: {
                normal: {
                    color: new echarts.graphic.LinearGradient(
                        0, 0, 0, 1, [{
                            offset: 0,
                            color: 'rgba(82,82,251,1)'
                        },
                        {
                            offset: 0.5,
                            color: 'rgba(82,82,251,0.75)'
                        },
                        {
                            offset: 1,
                            color: 'rgba(82,82,251,0)'
                        }
                    ]
                    )
                }
            },
            data: values

        }]
    }
    const chart = echarts.init(document.getElementById('chart'))
    chart.setOption(option)
}
export { requestPointData, formateData, renderChart }
